#include "LightAndMaterialShader.h"
#include "LightAndMaterialVS.h"
#include "LightAndMaterialPS.h"
LightAndMaterialShader::LightAndMaterialShader()
{

}
LightAndMaterialShader::~LightAndMaterialShader()
{

}

void LightAndMaterialShader::Initialize(ID3D11Device* device)
{
	HRESULT result;

	//The vertex shader and the pixel shader should have been pre-compiled and stored in .h files, so we directly read them from the variables defined in the .h files.
	result = device->CreateVertexShader(vertexShaderByteCode, sizeof(vertexShaderByteCode), nullptr, &vertexShader);
	result = device->CreatePixelShader(pixelShaderByteCode, sizeof(pixelShaderByteCode), nullptr, &pixelShader);

	//A layout is a description of a vertex structure.
	D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
	polygonLayout[0].SemanticName = "Position";
	polygonLayout[0].SemanticIndex = 0;
	polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
	polygonLayout[0].InputSlot = 0;
	polygonLayout[0].AlignedByteOffset = 0;
	polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	polygonLayout[0].InstanceDataStepRate = 0;
	polygonLayout[1].SemanticName = "Normal";
	polygonLayout[1].SemanticIndex = 0;
	polygonLayout[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
	polygonLayout[1].InputSlot = 0;
	polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	polygonLayout[1].InstanceDataStepRate = 0;
	polygonLayout[2].SemanticName = "Texcoord";
	polygonLayout[2].SemanticIndex = 0;
	polygonLayout[2].Format = DXGI_FORMAT_R32G32_FLOAT;
	polygonLayout[2].InputSlot = 0;
	polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	polygonLayout[2].InstanceDataStepRate = 0;
	result = device->CreateInputLayout(polygonLayout, ARRAYSIZE(polygonLayout), vertexShaderByteCode, sizeof(vertexShaderByteCode), &layout);

	D3D11_BUFFER_DESC vertexShaderBufferDesc;
	vertexShaderBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	vertexShaderBufferDesc.ByteWidth = sizeof(VertexShaderBufferType);
	vertexShaderBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	vertexShaderBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	vertexShaderBufferDesc.MiscFlags = 0;
	vertexShaderBufferDesc.StructureByteStride = 0;
	device->CreateBuffer(&vertexShaderBufferDesc, NULL, &m_vertexShaderBuffer);

	D3D11_BUFFER_DESC pixelShaderBufferDesc;
	pixelShaderBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	pixelShaderBufferDesc.ByteWidth = sizeof(PixelShaderBufferType);
	pixelShaderBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	pixelShaderBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	pixelShaderBufferDesc.MiscFlags = 0;
	pixelShaderBufferDesc.StructureByteStride = 0;
	result = device->CreateBuffer(&pixelShaderBufferDesc, NULL, &m_pixelShaderBuffer);
}
void LightAndMaterialShader::Shutdown()
{
	if (m_vertexShaderBuffer)
	{
		m_vertexShaderBuffer->Release();
		m_vertexShaderBuffer = 0;
	}
	if (m_pixelShaderBuffer)
	{
		m_pixelShaderBuffer->Release();
		m_pixelShaderBuffer = 0;
	}
	if (layout)
	{
		layout->Release();
		layout = 0;
	}
	if (pixelShader)
	{
		pixelShader->Release();
		pixelShader = 0;
	}
	if (vertexShader)
	{
		vertexShader->Release();
		vertexShader = 0;
	}
}